clear

set more off
set mem 100m


local EVENT  "majorinitiative1summit" /*majorinitiative1, majorinitiative2, majorinitiative1summit, majorinitiative2alt, decevent, compevent, or debtreliefevent*/
local thintrade    = 0 /*0: Do not deal with thin trading issues, 1 deal with  them using trade-to-trade returns*/
local controlevents = 1  /*Whether to include dummies for the corporate events occuring during the relevant periods*/
local controlindustry = 1 /*Whether to include a industrial index in the baseline model (improves the fit significanlty*/
local robust = 1 /*Use longest estimation window (There are 1-7 for the non-thin trade case only)*/
local estwindow 	= 180 /*Calender days*/
local evtwindow 	= 30 /*Calender days: this does not define the length of the window for the actual estimation, just for calling the file. I'm using a window of 10 in line 42*/

use Data_Event_Study_final_thin`thintrade'_est`estwindow'_evt`evtwindow'_control`controlevents'_ind`controlindustry'_`EVENT', replace


if "`EVENT'"=="majorinitiative1" | "`EVENT'"=="majorinitiative1summit" {
	drop if ticker=="GND" & eventyear==1996
    drop if event_time<-122 | event_time==. /*To be comparable to the portfolio approach*/
}

if `thintrade'== 0 {
	local thin = ""
}
else if `thintrade'== 1 {
	local thin = "tt"
}

replace AbnormalReturns`thin'd = AbnormalReturns`thin'd*100

keep ticker date panel_id estimation_window event_window event_time AbnormalReturns`thin' AbnormalReturns`thin'd eventyear  R Rd

drop if (estimation_window==. & event_window==.) | event_time==.


//*Compute cumulative average abnormal returns*/

sort panel_id event_time
egen avgAbnR`thin'd = mean(AbnormalReturns`thin'd), by (eventyear event_time)
egen avgAbnR`thin'd_all = mean(AbnormalReturns`thin'd), by (event_time)

sort eventyear event_time panel_id
by eventyear event_time: gen time_id = [_n] if event_window==1
sort event_time panel_id
by event_time: gen time_id_all = [_n] if event_window==1

egen onepereventyear = tag(eventyear event_time)
egen onepereventall  = tag(event_time)

gen cumavgAbnR`thin'd_event = .
gen tmp = .

*sort eventyear event_time panel_id

sort onepereventyear eventyear event_time

by onepereventyear eventyear: replace cumavgAbnR`thin'd_event = sum(avgAbnR`thin'd) if onepereventyear==1 & event_window==1

sort onepereventall event_time

by onepereventall: replace tmp = sum(avgAbnR`thin'd_all) if onepereventall==1 & event_window==1

egen cumavgAbnR`thin'd_all = mean(tmp), by(event_time)

tsset

if `thintrade'==0 {
	gen isatrade = 0 if Rd~=.
	replace isatrade = 1 if Rd~=0
}
else if `thintrade'==1 {
	gen isatrade=panel_id~=.
}

collapse avg* cumavg* estimation_window event_window (count) nfirms=panel_id (sum) isatrade, by(eventyear event_time)

if `thintrade'==0 {
	replace nfirms = isatrade
	egen nfirms_all = sum(isatrade), by(event_time)
}
else if `thintrade'== 1 {
	egen nfirms_all = sum(nfirms), by(event_time)
}

egen tmp = median(nfirms) if event_window==1, by(eventyear)
egen tmp2 = median(nfirms_all) if event_window==1
egen mednfirms = min(tmp), by(eventyear)
egen mednfirms_all = min(tmp2)
drop tmp tmp2


sort eventyear event_window event_time
by eventyear event_window: gen T = _n if event_window==1


sort eventyear event_time

egen tmp = sd(avgAbnR`thin'd) if estimation_window==1  & event_time>=-100 &  nfirms>=mednfirms, by(eventyear)
egen stderr_CAARd_event = min(tmp), by(eventyear)
replace stderr_CAARd_event = sqrt(T)*stderr_CAARd_event
drop tmp

egen onepereventdate = tag(event_time)
egen tmp = sd(avgAbnR`thin'd_all) if estimation_window==1 & onepereventdate==1  & event_time>=-120 & nfirms_all>=mednfirms_all, by(eventyear)
egen stderr_CAARd_all = min(tmp)
replace stderr_CAARd_all = sqrt(T)*stderr_CAARd_all
drop tmp

gen ciu_CAARd_event = cumavgAbnR`thin'd_event + stderr_CAARd_event*1.64
gen cil_CAARd_event = cumavgAbnR`thin'd_event - stderr_CAARd_event*1.64
gen ciu_CAARd_all = cumavgAbnR`thin'd_all + stderr_CAARd_all*1.64
gen cil_CAARd_all = cumavgAbnR`thin'd_all - stderr_CAARd_all*1.64
gen tstat_CAARd_event = cumavgAbnR`thin'd_event/stderr_CAARd_event
gen tstat_CAARd_all = cumavgAbnR`thin'd_all/stderr_CAARd_all

*COMPUTE p-value
gen pvalued_event = (1-normal(abs(tstat_CAARd_event)))*2
gen pvalued_all = (1-normal(abs(tstat_CAARd_all)))*2


preserve

keep if event_window==1

if `thintrade'==1 & "`EVENT'"== "majorinitiative1summit" {
    local EVENT "major1s"
}



*PLOT THESE WITH 90% CONFIDENCE INTERVAL BANDS AGAINST EVENT TIME
graph drop _all

twoway (line cumavgAbnR`thin'd_event event_time, lwidth(thick) ) (line ciu_CAARd_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
(line cil_CAARd_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) if eventyear==1996, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative abnormal return, margin(medium)) xtitle(Event time, margin(medium)) legend(off)  name(CAAR`thin'd_96_`EVENT')
gr save Figure_4_Panel_B, replace

twoway (line cumavgAbnR`thin'd_event event_time, lwidth(thick) ) (line ciu_CAARd_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
(line cil_CAARd_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) if eventyear==1999, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative abnormal return, margin(medium)) xtitle(Event time, margin(medium)) legend(off)  name(CAAR`thin'd_99_`EVENT')
gr save Figure_4_Panel_C, replace

twoway (line cumavgAbnR`thin'd_event event_time, lwidth(thick) ) (line ciu_CAARd_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) ///
(line cil_CAARd_event event_time, lpattern(shortdash) lwidth(medthick) lcolor(red)) if eventyear==2005, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative abnormal return, margin(medium)) xtitle(Event time, margin(medium)) legend(off) name( CAAR`thin'd_05_`EVENT')
gr save Figure_4_Panel_D, replace

twoway (line cumavgAbnR`thin'd_all event_time, lwidth(thick) ) (line ciu_CAARd_all event_time, lpattern(vshortdash) lwidth(medthick) lcolor(red)) ///
(line cil_CAARd_all event_time, lpattern(vshortdash) lwidth(medthick) lcolor(red)) if onepereventdate==1, ///
yline(0, lwidth(vvthin)) ytitle(Cumulative abnormal return, margin(medium)) xtitle(Event time, margin(medium)) legend(off)  name(CAAR`thin'd_all_`EVENT')
gr save Figure_4_Panel_A, replace


if `thintrade'== 0 {
	saveold CAR_Figure_4, replace
}

restore
